package cn.stronglink.asset.terminal.bjc.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetExtMaintenance;
import cn.stronglink.asset.model.AssetExtUsing;
import cn.stronglink.asset.model.AssetModel;
import cn.stronglink.asset.model.AssetOut;
import cn.stronglink.asset.model.AssetOutDetail;
import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.AssetType;
import cn.stronglink.asset.model.DataCenter;
import cn.stronglink.asset.model.Department;
import cn.stronglink.asset.model.InventorySheet;
import cn.stronglink.asset.model.InventorySheetDetail;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.ItAssetExtPosition;
import cn.stronglink.asset.model.PatrolItems;
import cn.stronglink.asset.model.Preset;
import cn.stronglink.asset.model.PresetDetail;
import cn.stronglink.asset.model.PullOff;
import cn.stronglink.asset.model.PullOffDetail;
import cn.stronglink.asset.model.PutOn;
import cn.stronglink.asset.model.PutOnDetail;
import cn.stronglink.asset.model.Status;
import cn.stronglink.asset.model.TRoom;
import cn.stronglink.asset.model.User;
import cn.stronglink.asset.model.UsingApply;
import cn.stronglink.asset.model.UsingApplyDetail;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.instock.mapper.AssetExtMaintenanceMapper;
import cn.stronglink.asset.module.assetmanage.instock.mapper.PresetDetailMapper;
import cn.stronglink.asset.module.assetmanage.instock.mapper.PresetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetExtPositionMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.itasset.vo.ItAssetVo;
import cn.stronglink.asset.module.assetmanage.out.mapper.OutMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.mapper.PullOffMapper;
import cn.stronglink.asset.module.assetmanage.pulloff.vo.PullOffDetailVo;
import cn.stronglink.asset.module.assetmanage.puton.mapper.PutOnMapper;
import cn.stronglink.asset.module.assetmanage.using.mapper.AssetExtUsingMapper;
import cn.stronglink.asset.module.assetmanage.using.mapper.UsingMapper;
import cn.stronglink.asset.module.basicdata.assetModel.mapper.AssetModelMapper;
import cn.stronglink.asset.module.basicdata.assetStatus.mapper.AssetStatusMapper;
import cn.stronglink.asset.module.basicdata.assetType.mapper.AssetTypeMapper;
import cn.stronglink.asset.module.basicdata.datacenter.mapper.DataCenterMapper;
import cn.stronglink.asset.module.basicdata.tRoom.mapper.TRoomMapper;
import cn.stronglink.asset.module.sys.department.mapper.DepartmentMapper;
import cn.stronglink.asset.module.sys.department.vo.DepartmentTreeNodeVo;
import cn.stronglink.asset.module.sys.user.mapper.UserMapper;
import cn.stronglink.asset.terminal.bjc.mapper.inventory.InventoryBjcMapper;
import cn.stronglink.asset.terminal.bjc.mapper.patrol.PatrolAPIMapper;
import cn.stronglink.asset.terminal.bjc.model.AssetModelVO;
import cn.stronglink.asset.terminal.bjc.model.InventorySheetDetailVo;
import cn.stronglink.asset.terminal.bjc.model.InventorySheetVo;
import cn.stronglink.asset.terminal.bjc.model.PatrolIitemsVo;
import cn.stronglink.asset.terminal.bjc.model.PatrolVo;
import cn.stronglink.asset.terminal.bjc.model.UploadInventoryParam;
import cn.stronglink.suite.core.util.WebUtil;

/**
 * @author sunlei:
 * @version 创建时间：2018年1月3日 下午12:24:05 类说明 北京海关手持机 service
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PdaService {
	@Autowired
	private TRoomMapper roomMapper;
	@Autowired
	private AssetStatusMapper statusMapper;
	@Autowired
	private AssetTypeMapper typeMapper;
	@Autowired
	private ItassetMapper assetMapper;
	@Autowired
	private ItassetExtPositionMapper assetPositionMapper;
	@Autowired
	private PresetMapper presetMapper;
	@Autowired
	private PresetDetailMapper presetDetailMapper;
	@Autowired
	private PutOnMapper putOnMapper;
	@Autowired
	private PullOffMapper pullOffMapper;
	@Autowired
	private OutMapper outMapper;
	@Autowired
	private InventoryBjcMapper inventoryMapper;
	@Autowired
	private DataCenterMapper dataCenterMapper;
	@Autowired
	private AssetModelMapper modelMapper;
	@Autowired
	private PatrolAPIMapper patrolMapper;
	@Autowired
	private DepartmentMapper deptMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private UsingMapper usingMapper;
	@Autowired
	private AssetExtUsingMapper assetUsingMapper;
	@Autowired
	private AssetExtMaintenanceMapper assetMaintenanceMapper;
	@Autowired
	private AssetRecordMapper assetRecordMapper;

	/***
	 * 下载机房
	 */
	public List<TRoom> getRooms() {
		return roomMapper.queryRoomList();
	}

	/***
	 * 下载状态
	 */
	public List<Status> getStatus() {
		return statusMapper.queryAllStatus();
	}

	/***
	 * 下载类型
	 */
	public List<AssetType> getAssetType() {
		return typeMapper.getListAllNotRack();
	}

	/***
	 * 下载数据中心
	 */
	public List<DataCenter> getDataCenter() {
		return dataCenterMapper.queryAll();
	}

	/***
	 * 下载机柜
	 */
	public List<ItAssetVo> getAllRack() {
		return assetMapper.getAllRacks();
	}

	/***
	 * 下载型号
	 */
	public List<AssetModelVO> getModel() {
		return modelMapper.downloadModel();
	}

	/***
	 * 下载部门
	 */
	public List<DepartmentTreeNodeVo> getDeptment() {
		return deptMapper.qryDepts();
	}

	/***
	 * 下载用户
	 */
	public List<User> getUser() {
		return userMapper.qryAllUser();
	}

	/***
	 * 通过sn号获取资产信息
	 * 
	 * @return
	 */
	public List<ItAssetVo> getAssetBySN(String snCode) {
		return assetMapper.getAssetBySN(snCode);
	}

	/***
	 * 通过rfid号获取资产信息
	 * 
	 * @return
	 */
	public List<ItAssetVo> getAssetByRfid(String rfidCode) {
		return assetMapper.getAssetByRfid(rfidCode);
	}

	/***
	 * 获取未发卡的资产
	 * 
	 * @return
	 */
	public List<ItAssetVo> getBindAsset() {
		return assetMapper.getBindAsset();
	}

	/***
	 * 发卡
	 * 
	 * @param assetId
	 *            资产id
	 * @param rfid
	 *            rfid
	 * @return
	 */
	public String bindRfid(String assetId, String rfid) {

		int count = assetMapper.bindRfid(assetId, rfid);
		return count > 0 ? "绑定成功！" : "绑定失败！";
	}

	/***
	 * 同步到货验收单
	 * 
	 * @param preset
	 * @return
	 */
	public List<Preset> getPresetList() {
		List<Preset> list = presetMapper.getPresetList();
		if (list.size() > 0) {
			List<PresetDetail> detailList = null;
			Preset preset = null;
			for (int i = 0; i < list.size(); i++) {
				preset = list.get(i);
				long pid = preset.getId();
				detailList = presetDetailMapper.queryBjcDetailList(pid);
				preset.setDetailList(detailList);
				preset.setStatus(2);
				presetMapper.updatePreset(preset);
			}
		}
		return list;
	}

	/***
	 * 上传到货验收
	 * 
	 * @param preset
	 * @return
	 */
	public void uploadPreset(List<Preset> presetList) {
		if (presetList.size() > 0) {
			Preset preset = null;
			List<PresetDetail> detailList = null;
			for (int i = 0; i < presetList.size(); i++) {
				preset = presetList.get(i);
				preset.setStatus(3);
				preset.setUpdateTime(new Date());
				presetMapper.updatePreset(preset);
				Long userId = preset.getUpdateBy();
				detailList = preset.getDetailList();
				preset = presetMapper.qryById(preset.getId());
				if (detailList.size() > 0) {
					PresetDetail detail = null;
					ItAsset asset = null;
					ItAssetExtPosition assetPosition = null;
					AssetExtUsing assetExtUsing = null;
					AssetExtMaintenance extMaintenance = null;
					for (int j = 0; j < detailList.size(); j++) {
						detail = detailList.get(j);
						Long manageBranchId = detail.getManageBranchId();// 管理部门id
						String hotline = detail.getHotline();// 售后电话
						String invoiceNo = detail.getInvoiceNo();// 发票号
						Date buyTime = detail.getBuyTime();// 购置时间
						Date expirationTime = detail.getExpirationTime();// 保修到期时间
						detail.setStatus(2);
						detail.setUpdateTime(new Date());
						detail.setUpdateBy(userId);
						presetDetailMapper.updatePresetDetail(detail);

						detail = presetDetailMapper.qryDetailById(detail.getId());
						asset = new ItAsset();
						asset.setId(IdWorker.getId());
						asset.setAssetNo(detail.getAssetNo());
						asset.setAssetName(detail.getAssetName());
						asset.setSerialNo(detail.getSerialNo());
						asset.setRfidNo(detail.getRfidNo());
						asset.setTypeId(detail.getTypeId());
						asset.setBrandId(detail.getBrandId());
						asset.setModelId(detail.getModelId());
						asset.setStatus(1);// 在库
						asset.setIsDistribute(false);
						asset.setIsBorrow(false);
						asset.setCreateTime(new Date());
						asset.setCreateBy(userId);
						assetMapper.insertItAsset(asset);// 把设备插库

						assetExtUsing = new AssetExtUsing();
						assetExtUsing.setId(IdWorker.getId());
						assetExtUsing.setAssetId(asset.getId());
						assetExtUsing.setCreateBy(userId);
						assetExtUsing.setCreateTime(new Date());
						assetExtUsing.setManageBranchId(manageBranchId);
						assetUsingMapper.insertAssetExtUsing(assetExtUsing);// 把设备扩展信息插库

						assetPosition = new ItAssetExtPosition();
						assetPosition.setId(IdWorker.getId());
						assetPosition.setAssetId(asset.getId());
						assetPosition.setRoomId(preset.getRoomId());
						assetPosition.setCreateBy(userId);
						assetPosition.setCreateTime(new Date());
						assetPositionMapper.insertItAssetExtPosition(assetPosition);

						extMaintenance = new AssetExtMaintenance();
						extMaintenance.setId(IdWorker.getId());
						extMaintenance.setAssetId(asset.getId());
						extMaintenance.setSupplier(preset.getSupplier());
						extMaintenance.setContactsPhone(preset.getContactsPhone());
						extMaintenance.setContacts(preset.getContacts());
						extMaintenance.setHotline(hotline);
						extMaintenance.setContractNo(preset.getContractNo());
						extMaintenance.setInvoiceNo(invoiceNo);
						extMaintenance.setPrice(detail.getPrice());
						extMaintenance.setBuyTime(buyTime);
						extMaintenance.setExpirationTime(expirationTime);
						extMaintenance.setCreateBy(userId);
						extMaintenance.setCreateTime(new Date());
						assetMaintenanceMapper.insertAssetExtMaintenance(extMaintenance);

						// 插入资产生命周期表记录
						AssetRecord re = new AssetRecord();
						re.setId(IdWorker.getId());
						re.setAssetId(asset.getId());
						re.setType(1);
						re.setCreateBy(WebUtil.getCurrentUser());
						re.setCreateTime(new Date());
						assetRecordMapper.create(re);
					}
				}
			}
		}
	}

	/***
	 * 设备到货
	 * 
	 * @param preset
	 * @return
	 */

	public void assetArrival(Preset preset) {
		List<PresetDetail> detailList = preset.getDetailList();
		if (detailList.size() > 0) {
			preset.setId(IdWorker.getId());
			preset.setCreateTime(new Date());
			preset.setStatus(3);// 已完成
			presetMapper.insertPreset(preset);// 创建预置、验收单

			ItAsset asset = null;
			PresetDetail detail = null;
			ItAssetExtPosition assetPosition = null;
			AssetExtUsing assetExtUsing = null;
			Date now = new Date();
			for (int i = 0; i < detailList.size(); i++) {

				detail = detailList.get(i);
				detail.setId(IdWorker.getId());
				detail.setPresetId(preset.getId());
				detail.setCreateTime(now);
				detail.setStatus(2);// 入库
				presetDetailMapper.insertPresetDetail(detail);// 创建预置、验收 详情单

				asset = new ItAsset();
				asset.setId(IdWorker.getId());
				asset.setAssetNo(detail.getAssetNo());
				asset.setAssetName(detail.getAssetName());
				asset.setSerialNo(detail.getSerialNo());
				asset.setTypeId(detail.getTypeId());
				asset.setBrandId(detail.getBrandId());
				asset.setModelId(detail.getModelId());
				asset.setRemark(detail.getRemark());
				asset.setStatus(1);// 在库
				asset.setIsDistribute(false);
				asset.setIsBorrow(false);
				asset.setCreateTime(now);
				asset.setCreateBy(detail.getCreateBy());
				assetMapper.insertItAsset(asset);// 把设备插库

				assetExtUsing = new AssetExtUsing();
				assetExtUsing.setId(IdWorker.getId());
				assetExtUsing.setAssetId(asset.getId());
				assetExtUsing.setCreateBy(detail.getCreateBy());
				assetExtUsing.setCreateTime(now);
				assetExtUsing.setManageBranchId(detail.getManageBranchId());
				assetUsingMapper.insertAssetExtUsing(assetExtUsing);// 把设备扩展信息插库

				assetPosition = new ItAssetExtPosition();
				assetPosition.setId(IdWorker.getId());
				assetPosition.setAssetId(asset.getId());
				assetPosition.setCreateBy(detail.getCreateBy());
				assetPosition.setCreateTime(now);
				assetPositionMapper.insertItAssetExtPosition(assetPosition);
			}
		}
	}

	/***
	 * 设备上架 手持上架 是否需要判断设备是 已分配的
	 */

	public void assetPutOn(PutOn putOn) {
		List<PutOnDetail> detail = putOn.getDetailList();
		if (detail.size() > 0) {
			Date now = new Date();
			putOn.setId(IdWorker.getId());
			putOn.setType(1);// 普通上架
			putOn.setStatus(3);// 已完成
			putOn.setExecuteTime(now);
			putOn.setOperatorBy(putOn.getCreateBy());
			putOn.setCreateTime(now);
			putOn.setRemark("手持机上架");
			putOnMapper.insertPutOn(putOn);// 创建上架单
			PutOnDetail putOnDetail = null;
			ItAsset asset = null;
			ItAssetExtPosition assetPosition = null;
			for (int i = 0; i < detail.size(); i++) {
				putOnDetail = detail.get(i);
				putOnDetail.setId(IdWorker.getId());
				putOnDetail.setAssetId(Long.parseLong(putOnDetail.getAssetId_()));
				putOnDetail.setPutOnId(putOn.getId());
				putOnDetail.setStatus(1);// 已上架
				putOnDetail.setCreateTime(now);
				putOnDetail.setCreateBy(putOn.getCreateBy());
				putOnMapper.insertPutOnDetail(putOnDetail);// 创建上架单明细

				asset = new ItAsset();
				asset.setId(putOnDetail.getAssetId());
				asset.setUpdateBy(putOnDetail.getCreateBy());
				asset.setUpdateTime(now);
				asset.setStatus(2);// 更新资产状态 为已上架
				assetMapper.updateItAssetById(asset);

				assetPosition = new ItAssetExtPosition();
				assetPosition.setAssetId(putOnDetail.getAssetId());
				assetPosition.setRoomId(putOn.getRoomId());
				assetPosition.setRackId(Long.parseLong(putOnDetail.getRackId()));
				assetPosition.setStart(putOnDetail.getStartU());
				assetPosition.setEnd(putOnDetail.getEndU());
				assetPositionMapper.updateItAssetExtPositionByAssetId(assetPosition);

				// 插入资产生命周期表记录
				AssetRecord re = new AssetRecord();
				re.setId(IdWorker.getId());
				re.setAssetId(Long.parseLong(putOnDetail.getAssetId_()));
				re.setType(2);
				re.setCreateBy(WebUtil.getCurrentUser());
				re.setCreateTime(new Date());
				assetRecordMapper.create(re);
			}
		}
	}

	/***
	 * 设备下架
	 */
	public void assetPullOff(PullOff pullOff) {
		List<PullOffDetailVo> detail = pullOff.getDetailList();
		if (detail.size() > 0) {
			Date now = new Date();
			pullOff.setId(IdWorker.getId());
			pullOff.setExecuteTime(now);
			pullOff.setType(1);// 普通下架
			pullOff.setStatus(3);// 已完成
			Long uid = pullOff.getCreateBy();
			pullOff.setOperatorBy(uid);
			pullOff.setCreateTime(now);
			pullOff.setRemark("手持机下架");
			pullOffMapper.insertPullOff(pullOff);// 创建下架单

			PullOffDetailVo pullOffDetail = null;
			ItAsset asset = null;
			ItAssetExtPosition assetPosition = null;
			for (int i = 0; i < detail.size(); i++) {
				pullOffDetail = detail.get(i);
				pullOffDetail.setId(IdWorker.getId());
				pullOffDetail.setAssetId(Long.parseLong(pullOffDetail.getAssetId_()));
				pullOffDetail.setPullOffId(pullOff.getId());
				pullOffDetail.setStatus(1);// 已下架
				pullOffDetail.setCreateTime(now);
				pullOffDetail.setCreateBy(pullOff.getCreateBy());
				pullOffMapper.insertPullOffDetail(pullOffDetail);

				asset = new ItAsset();
				asset.setId(pullOffDetail.getAssetId());
				asset.setUpdateBy(pullOffDetail.getCreateBy());
				asset.setUpdateTime(now);
				asset.setStatus(1);// 更新资产状态 为库存
				assetMapper.updateItAssetById(asset);// 更新资产

				assetPosition = new ItAssetExtPosition();
				assetPosition.setAssetId(pullOffDetail.getAssetId());
				assetPosition.setRoomId(pullOff.getRoomId());
				// assetPosition.setRackId(null);
				// assetPosition.setStart(null);
				assetPosition.setUpdateBy(pullOffDetail.getCreateBy());
				assetPosition.setUpdateTime(now);
				assetPositionMapper.updateItAssetExtPositionByAssetId(assetPosition);// 更新资产位置信息
				// 插入资产生命周期表记录
				AssetRecord re = new AssetRecord();
				re.setId(IdWorker.getId());
				re.setAssetId(Long.parseLong(pullOffDetail.getAssetId_()));
				re.setType(3);
				re.setCreateBy(WebUtil.getCurrentUser());
				re.setCreateTime(new Date());
				assetRecordMapper.create(re);
			}
		}
	}

	/***
	 * 设备出库
	 */
	public void assetOut(AssetOut assetOut) {
		List<AssetOutDetail> detail = assetOut.getAssetOutDetails();
		if (detail.size() > 0) {
			Date now = new Date();
			assetOut.setId(IdWorker.getId());
			assetOut.setStatus(3);// 已完成
			assetOut.setCreateTime(now);
			Long uid = assetOut.getCreateBy();
			assetOut.setOperatorBy(uid);
			assetOut.setRemark("手持机出库");
			outMapper.create(assetOut);// 创建出库单

			AssetOutDetail outDetail = null;
			ItAsset asset = null;
			ItAssetExtPosition assetPosition = null;
			for (int i = 0; i < detail.size(); i++) {
				outDetail = detail.get(i);
				outDetail.setId(IdWorker.getId());
				outDetail.setAssetId(Long.parseLong(outDetail.getAssetId_()));
				outDetail.setOutId(assetOut.getId());
				outDetail.setStatus(1);// 已出库
				outDetail.setCreateTime(now);
				outMapper.createDetail(outDetail);

				asset = new ItAsset();
				asset.setId(Long.parseLong(outDetail.getAssetId_()));
				asset.setUpdateBy(outDetail.getCreateBy());
				asset.setUpdateTime(now);
				asset.setStatus(4);// 更新资产状态 为出库
				assetMapper.updateItAssetById(asset);// 更新资产

				assetPosition = new ItAssetExtPosition();
				assetPosition.setAssetId(Long.parseLong(outDetail.getAssetId_()));
				// assetPosition.setRoomId(null);
				// assetPosition.setRackId(null);
				// assetPosition.setStart(null);
				assetPosition.setUpdateBy(outDetail.getCreateBy());
				assetPosition.setUpdateTime(now);
				assetPositionMapper.updateItAssetExtPositionByAssetId(assetPosition);// 更新资产位置信息
			}
		}
	}

	/***
	 * 获取盘点单
	 */
	public List<InventorySheetVo> qryInventorySheetList() {
		List<InventorySheetVo> list = inventoryMapper.qryInventorySheetList();// 得到sheetId
																				// 计划名
																				// 和机房名称
		if (list != null && list.size() > 0) {
			for (InventorySheetVo sheet : list) {
				List<InventorySheetDetailVo> detailList = inventoryMapper
						.qryInventorySheetDetail(Long.parseLong(sheet.getInventorySheetId_()));
				// 返回盘点单中机柜
				sheet.setListData(detailList);
			}
		}
		return list;
	}

	/***
	 * 上传盘点
	 */
	public void uploadInventory(List<UploadInventoryParam> paramList) {
		Set<Long> sheetIdList = new HashSet<Long>();
		for (UploadInventoryParam obj : paramList) {
			sheetIdList.add(obj.getInventorySheetId());
		}

		if (sheetIdList != null && sheetIdList.size() > 0) {
			List<InventorySheet> sheetList = new ArrayList<InventorySheet>();
			for (Long sheetId : sheetIdList) {
				InventorySheet sheet = new InventorySheet();
				sheet.setId(sheetId);
				sheet.setStatus(2);
				sheet.setExecuteTime(new Date());
				sheet.setUpdateTime(new Date());
				// sheet.setCreateBy(879224697238376448l);
				sheetList.add(sheet);
			}
			// 批量更新盘点单为已完成
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("sheetList", sheetList);
			inventoryMapper.updateInventorySheetBatch(map);

			// 先将盘点单里的盘点详情都更新成盘亏状态
			HashMap<String, Object> detailmap = new HashMap<String, Object>();
			detailmap.put("sheetIdList", sheetIdList);
			inventoryMapper.uploadDetailBySheetId(detailmap);

		}

		for (UploadInventoryParam param : paramList) {
			// 手持那边肯定 存了 盘点详情数据了 详情id 盘点id 资产 信息等
			InventorySheetDetail detail = new InventorySheetDetail();
			if (!"".equals(param.getInventorySheetDetailId()) && param.getInventorySheetDetailId() != null
					&& !"".equals(param.getCode900M()) && param.getCode900M() != null) {
				// 判断如果当前设备的900M标签对不上当前的设备，则是差异
				InventorySheetDetail d = inventoryMapper.qryDetailById(param.getInventorySheetDetailId());
				// detail.setId(Long.parseLong(param.getInventorySheetDetailId()));
				// detail.setStatus(2);//差异
				// detail.setUpdateBy(879224697238376448l);
				// detail.setUpdateTime(new Date());
				if (d != null) {
					if (!"".equals(d.getRfidNo()) && d.getRfidNo() != null) {
						if (d.getRfidNo().equals(param.getCode900M()) || d.getRfidNo() == param.getCode900M()) {
							d.setStatus(3);// 正常
						}
					}
					inventoryMapper.uploadInventorySheetDetail(d);
				} else {
					List<ItAssetVo> assetList = assetMapper.getAssetByRfid(param.getCode900M());
					if (assetList.size() > 0) {// 如果该rfid有效，才算盘盈
						detail.setId(IdWorker.getId());
						detail.setInventorySheetId(param.getInventorySheetId());
						detail.setRfidNo(param.getCode900M());
						detail.setActualRoom(assetList.get(0).getRoomId());
						detail.setActualCabinet(assetList.get(0).getRackId());
						detail.setActualU(assetList.get(0).getStartU());
						detail.setLedgerRoom(assetList.get(0).getRoomId());
						detail.setLedgerCabinet(assetList.get(0).getRackId());
						detail.setLedgerU(assetList.get(0).getStartU());
						detail.setIsHandle(1);
						detail.setStatus(5);
						detail.setCreateTime(new Date());
						inventoryMapper.insertInventorySheetDetailOne(detail);
					}
				}

			}
		}

	}

	/***
	 * 同步巡检
	 */
	public List<PatrolVo> qryPatrol(Map<String, Object> params) {
		List<PatrolVo> list = patrolMapper.queryPatrolList(params);
		if (list.size() > 0) {
			PatrolVo patrolVo = null;
			for (int i = 0; i < list.size(); i++) {
				patrolVo = list.get(i);
				// List<PatrolItems> detailList =
				// patrolMapper.queryPatrolItems(patrolVo.getId());
				// if (detailList.size() > 0) {
				// patrolVo.setDetailList(detailList);
				patrolVo.setStatus(2);
				patrolMapper.updatePatrolById(patrolVo);// 手持 同步后 更改状态为 执行中
				// }
			}
		}
		return list;
	}

	/***
	 * 上传巡检
	 */
	public void uploadPatrol(List<PatrolVo> patroVoList) {
		if (patroVoList.size() > 0) {
			PatrolVo patrol = null;
			for (int i = 0; i < patroVoList.size(); i++) {
				patrol = patroVoList.get(i);
				Long uid = patrol.getOperatiorBy();
				patrol.setUpdateBy(uid);
				patrol.setUpdateTime(new Date());
				patrolMapper.updatePatrolById(patrol);// 从web端同步后 上传
				List<PatrolIitemsVo> itemList = patrol.getDetailList();
				if (itemList.size() > 0) {
					PatrolIitemsVo items = null;
					List<ItAssetVo> assetList = null;
					for (int j = 0; j < itemList.size(); j++) {
						items = itemList.get(j);
						Long assetId = null;
						Long rackId = null;
						if (items.getRfidNo() != null) {
							assetList = assetMapper.getAssetByRfid(items.getRfidNo());
						} else {
							assetList = assetMapper.getAssetBySN(items.getSerialNo());
						}
						if (assetList.size() > 0) {
							ItAssetVo assetVo = assetList.get(0);
							assetId = assetVo.getAssetId();
							rackId = assetVo.getRackId();
						}
						items.setAssetId(assetId);
						items.setRackId(rackId);
						items.setId(IdWorker.getId());
						items.setCreateBy(uid);
						items.setCreateTime(new Date());
						patrolMapper.insertPatrolItems(items);// 插入巡检项
					}
				}
			}
		}
	}

	/***
	 * 上传领用
	 */
	public void assetUsingApply(UsingApply using) {
		// TODO Auto-generated method stub
		List<UsingApplyDetail> detailList = using.getDetailList();
		if (detailList.size() > 0) {
			using.setId(IdWorker.getId());
			using.setCreateTime(new Date());
			using.setCount(1);
			using.setStatus(3);
			usingMapper.insertUsingApply(using);
			UsingApplyDetail detail = null;
			ItAsset asset = null;
			AssetExtUsing assetExtUsing = null;
			for (int i = 0; i < detailList.size(); i++) {
				detail = detailList.get(i);
				Long createBy = using.getCreateBy();
				Long assetId = detail.getAssetId();
				detail.setId(IdWorker.getId());
				detail.setUsingApplyId(using.getId());
				detail.setStatus(2);
				detail.setDesc("手持机领用分配！");
				detail.setCreateBy(createBy);
				detail.setCreateTime(new Date());
				usingMapper.insertUsingApplyDetail(detail);
				asset = new ItAssetVo();
				assetExtUsing = new AssetExtUsing();
				asset.setId(assetId);
				asset.setIsDistribute(true);
				asset.setUpdateTime(new Date());
				asset.setUpdateBy(createBy);
				assetMapper.updateItAsset(asset);

				assetExtUsing.setAssetId(assetId);
				assetExtUsing.setUseBranchId(using.getBranchId());
				assetExtUsing.setEndUser(using.getUserId());
				assetExtUsing.setUpdateBy(createBy);
				assetExtUsing.setUpdateTime(new Date());
				assetUsingMapper.updateAssetExtUsing(assetExtUsing);

			}
		}
	}

	/**
	 * 手持同步web上架单
	 * 
	 * @param params
	 *            操作人
	 * @return
	 */
	public List<PutOn> getPutOnList(Map<String, String> params) {
		String userId = params.get("operatiorBy_").toString();
		List<PutOn> list = putOnMapper.getPutOnList(userId);
		if (list.size() > 0) {
			PutOn puton = null;
			for (int i = 0; i < list.size(); i++) {
				puton = list.get(i);
				List<PutOnDetail> detailList = putOnMapper.getDetailById(puton.getId());
				if (detailList.size() > 0) {
					puton.setDetailList(detailList);
					puton.setStatus(2);
					putOnMapper.updatePutOn(puton);// 手持 同步后 更改状态为 执行中
				}
			}
		}
		return list;
	}

	/**
	 * 手持上传web上架单
	 * 
	 * @param putOnList
	 * @return
	 */
	public void uploadPutOn(List<PutOn> putOnList) {
		if (putOnList.size() > 0) {
			PutOn putOn = null;
			for (int i = 0; i < putOnList.size(); i++) {
				putOn = putOnList.get(i);
				Long userId = putOn.getOperatorBy();
				putOn.setUpdateBy(userId);
				putOn.setUpdateTime(new Date());
				putOn.setStatus(3);
				putOnMapper.updatePutOn(putOn);
				PutOnDetail putOnDetail = null;
				ItAsset asset = null;
				ItAssetExtPosition assetPosition = null;
				List<PutOnDetail> detailList = putOn.getDetailList();
				if (detailList.size() > 0) {
					for (int j = 0; j < detailList.size(); j++) {
						putOnDetail = detailList.get(j);
						putOnDetail.setUpdateBy(userId);
						putOnDetail.setUpdateTime(new Date());
						putOnDetail.setStatus(1);
						putOnMapper.updatePutOnDetail(putOnDetail);// 更新上架单明细

						Long assetId = Long.parseLong(putOnDetail.getAssetId_());
						asset = new ItAsset();
						asset.setId(assetId);
						asset.setUpdateBy(userId);
						asset.setUpdateTime(new Date());
						asset.setStatus(2);// 更新资产状态 为已上架
						assetMapper.updateItAssetById(asset);

						assetPosition = new ItAssetExtPosition();
						assetPosition.setAssetId(assetId);
						assetPosition.setRackId(Long.parseLong(putOnDetail.getRackId()));
						assetPosition.setStart(putOnDetail.getStartU());
						assetPosition.setEnd(putOnDetail.getEndU());// 更新资产位置信息
						assetPosition.setRoomId(Long.parseLong(putOnDetail.getRoomId()));
						assetPositionMapper.updateItAssetExtPositionByAssetId(assetPosition);
						
						// 插入资产生命周期表记录
						AssetRecord re = new AssetRecord();
						re.setId(IdWorker.getId());
						re.setAssetId(assetId);
						re.setType(2);
						re.setCreateBy(WebUtil.getCurrentUser());
						re.setCreateTime(new Date());
						assetRecordMapper.create(re);
					}
				}
			}
		}
	}

	/**
	 * 手持同步web下架单
	 * 
	 * @param params
	 *            操作人
	 * @return
	 */
	public List<PullOff> getPullOffList(Map<String, String> params) {
		// TODO Auto-generated method stub
		List<PullOff> list = pullOffMapper.getPullOffList(params);
		if (list.size() > 0) {
			PullOff pullOff = null;
			for (int i = 0; i < list.size(); i++) {
				pullOff = list.get(i);
				List<PullOffDetailVo> detailList = pullOffMapper.getDetailById(pullOff.getId());
				if (detailList.size() > 0) {
					pullOff.setDetailList(detailList);
					pullOff.setStatus(2);
					pullOffMapper.updatePullOff(pullOff);// 手持 同步后 更改状态为 执行中
				}
			}
		}
		return list;
	}

	/**
	 * 手持上传web下架单
	 * 
	 * @param putOnList
	 * @return
	 */
	public void uploadPullOff(List<PullOff> pullOffList) {
		// TODO Auto-generated method stub
		if (pullOffList.size() > 0) {
			PullOff pulloff = null;
			for (int i = 0; i < pullOffList.size(); i++) {
				pulloff = pullOffList.get(i);
				Long userId = pulloff.getOperatorBy();
				pulloff.setUpdateBy(userId);
				pulloff.setUpdateTime(new Date());
				pulloff.setStatus(3);
				pullOffMapper.updatePullOff(pulloff);
				PullOffDetailVo putoffDetail = null;
				ItAsset asset = null;
				ItAssetExtPosition assetPosition = null;
				List<PullOffDetailVo> detailList = pulloff.getDetailList();
				if (detailList.size() > 0) {
					for (int j = 0; j < detailList.size(); j++) {
						putoffDetail = detailList.get(j);
						putoffDetail.setUpdateBy(userId);
						putoffDetail.setUpdateTime(new Date());
						putoffDetail.setStatus(1);
						pullOffMapper.updatePullOffDetail(putoffDetail);// 更新下架单明细

						Long assetId = Long.parseLong(putoffDetail.getAssetId_());
						asset = new ItAsset();
						asset.setId(assetId);
						asset.setUpdateBy(userId);
						asset.setUpdateTime(new Date());
						asset.setStatus(1);// 更新资产状态 为在库
						assetMapper.updateItAssetById(asset);

//						assetPosition = new ItAssetExtPosition();
//						assetPosition.setAssetId(Long.parseLong(putoffDetail.getAssetId_()));
//						assetPosition.setRoomId(pulloff.getRoomId());
//					    assetPosition.setRackId(null);
//						assetPosition.setStart(null);
//						assetPosition.setUpdateBy(userId);
//						assetPosition.setUpdateTime(new Date());
//						assetPositionMapper.updateItAssetExtPositionByAssetId(assetPosition);// 更新资产位置信息
						
						// 插入资产生命周期表记录
						AssetRecord re = new AssetRecord();
						re.setId(IdWorker.getId());
						re.setAssetId(assetId);
						re.setType(3);
						re.setCreateBy(WebUtil.getCurrentUser());
						re.setCreateTime(new Date());
						assetRecordMapper.create(re);

					}
				}
			}
		}
	}

	public List<ItAssetVo> getAssetByAssetCode(String assetCode) {
		return assetMapper.getAssetByAssetCode(assetCode);
	}

}
